#include <iostream>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <stdlib.h>
#include <regex>
#include <string>
#include <cstring>
#include <unistd.h>
#include <typeinfo>
#include <sstream>
#include <chrono>
#include "filestream.cpp"
#include "split.cpp"
#include "sqlite3.cpp"

using namespace std;
using json=nlohmann::json;

string milliseconds()
{
    auto now        = std::chrono::system_clock::now();
    auto now_ms     = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
    auto value      = now_ms.time_since_epoch().count();
    string str_time =(to_string(value)).substr(10,-1);
    return str_time;
}

void timestart()
{
    const char* path        ="time.json";
    std::string date        ="2025-01-28 23:59:59";
    // std::getline(cin,date);
    struct tm timeinfo      ={};
    std::istringstream ss(date);
    ss>>std::get_time(&timeinfo,"%Y-%m-%d %H:%M:%S");
    long timestamp          =(long)std::mktime(&timeinfo);
    while(1)
    {
        long times          =(long)time(0);
        long ptime          =((timestamp-times)-28800)-1;
        std::tm local_time  =*std::localtime(&ptime);
        std::stringstream s;
        s << put_time(&local_time,"%Y %m %d %H %M %S");
        string addtime  =s.str();
        string fh       =" ";
        auto arr        =split(addtime,fh);
        string years    =to_string((atoi(arr[0].c_str()))-1970);

        string moons    =to_string(atoi(arr[1].c_str())-1);
        string suns     =to_string(atoi(arr[2].c_str())-1);

        if (moons.length()==0)      arr[1]="00";
        else if(moons.length()==1)  arr[1]="0"+moons;
        else if(moons.length()==2)  arr[1]=moons;
        if (suns.length()==0)       arr[2]="00";
        else if(suns.length()==1)   arr[2]="0"+suns;
        else if(suns.length()==2)   arr[2]=suns;
        if (years.length()==0)      arr[0]="0000";
        else if(years.length()==3)  arr[0]="0"+years;
        else if(years.length()==2)  arr[0]="00"+years;
        else if(years.length()==1)  arr[0]="000"+years;
        else                        arr[0]=years;

        json jt;
        for (int i = 0; i < 6; i++)
        {
            jt["date"][i]=arr[i];
        }
        
        string tj       =jt.dump();

        // int d           =file_put_contents(path, tj);

        string sql="UPDATE stime SET ctime='"+tj+"' where id=1;";
        sqlite3_idu(sql);

        if(ptime+28800<=0) break;

        //滴嗒定时器开始
        sleep(1);

    }


    
    


    
        
    
}

int main(int argc, char const *argv[])
{
    timestart();
    return 0;
}

